<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="120px">
      <el-form-item label="新闻标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入新闻标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input
          v-model="queryParams.author"
          placeholder="请输入作者"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否轮播" prop="isCarousel">
        <el-select v-model="queryParams.isCarousel" placeholder="请选择是否轮播" clearable size="small">
          <el-option
            v-for="dict in isCarouselOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="新闻是否置顶" prop="isTop">
        <el-select v-model="queryParams.isTop" placeholder="请选择新闻是否置顶" clearable size="small">
          <el-option
            v-for="dict in isTopOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="新闻栏目" prop="newsColumn">
        <el-select v-model="queryParams.newsColumn" placeholder="请选择新闻栏目" clearable size="small" @change="getColumn2Options">
          <el-option
            v-for="dict in newsColumnOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="新闻子栏目" prop="newsColumn2">
        <el-select v-model="queryParams.newsColumn2" placeholder="请选择新闻子栏目" clearable size="small">
          <el-option
            v-for="dict in newsColumn2Options"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basic:news:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basic:news:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basic:news:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basic:news:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="openDeptUserSelector"
          v-hasPermi="['basic:news:edit']"
        >发布</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="newsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="新闻标题" align="center" prop="title" />
      <el-table-column label="作者" align="center" prop="author" />
      <el-table-column label="是否轮播" align="center" prop="isCarousel" :formatter="isCarouselFormat" />
      <el-table-column label="轮播顺序" align="center" prop="carouselOrder" />
      <el-table-column label="新闻是否置顶" align="center" prop="isTop" :formatter="isTopFormat" />
      <el-table-column label="新闻栏目" align="center" prop="newsColumn" :formatter="newsColumnFormat" />
      <el-table-column label="新闻子栏目" align="center" prop="newsColumn2" :formatter="newsColumn2Format" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basic:news:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basic:news:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog class="admin-selector" title="请选择人员" :visible.sync="deptUserSelectorOpen" width="800">
      <qywx-dept-user-selector ref="userSelector" :selectedList="qywxDeptUserSelector.selectedList" @onSelectUser="setSelectedList" @onDelUser="delSelectedList" :isSelectDept="true" @onSelectDept="setSelectedList"></qywx-dept-user-selector>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleRelease">确 定</el-button>
        <el-button @click="closeDeptUserSelector">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listNews, delNews, exportNews, releaseNews } from "@/api/basic/news"
import QywxDeptUserSelector from "@/components/QywxDeptUserSelector"

export default {
  name: "News",
  components: { QywxDeptUserSelector },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 新闻公告表格数据
      newsList: [],
      // 弹出层标题
      title: "",
      // 是否轮播字典
      isCarouselOptions: [],
      // 新闻是否置顶字典
      isTopOptions: [],
      // 新闻栏目字典
      newsColumnOptions: [],
      // 新闻子栏目字典
      newsColumn2Options: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isActive: undefined,
        title: undefined,
        author: undefined,
        isCarousel: undefined,
        isTop: undefined,
        newsColumn: undefined,
        newsColumn2: undefined,
      },
      deptUserSelectorOpen:false,
      qywxDeptUserSelector:{
        newsId:"",
        selectedList:[]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("is_active").then(response => {
      this.isCarouselOptions = response.data;
      this.isTopOptions = response.data;
    });
    this.getLsDicts("LESSEE_NEWS_COLUMN").then(response => {
      this.newsColumnOptions = response.data;
    });
  },
  methods: {
    /** 查询新闻公告列表 */
    getList() {
      this.loading = true;
      listNews(this.queryParams).then(response => {
        this.newsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 是否轮播字典翻译
    isCarouselFormat(row, column) {
      return this.selectDictLabel(this.isCarouselOptions, row.isCarousel);
    },
    // 新闻是否置顶字典翻译
    isTopFormat(row, column) {
      return this.selectDictLabel(this.isTopOptions, row.isTop);
    },
    // 新闻栏目字典翻译
    newsColumnFormat(row, column) {
      return this.selectDictLabel(this.newsColumnOptions, row.newsColumn);
    },
    // 新闻子栏目字典翻译
    newsColumn2Format(row, column) {
      return this.selectDictLabel(this.newsColumn2Options, row.newsColumn2);
    },
    getColumn2Options(ColumnValue){
      this.getLsDicts(ColumnValue).then(response => {
        this.newsColumn2Options = response.data
        this.queryParams.newsColumn2 = undefined
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push('/JCGL/news/edit/new')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push('/JCGL/news/edit/'+(row.id||this.ids))
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除新闻公告编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delNews(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有新闻公告数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportNews(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    /** 发布按钮操作 */
    handleRelease(){
      let that = this
      let userIds = ""
      let depts = ""
      for(let i of this.qywxDeptUserSelector.selectedList){
        if(i.userId){
          if(userIds){
            userIds = userIds + "|" + i.userId
          } else {
            userIds = i.userId
          }
        }else{
          if(depts){
            depts = depts + "|" + i.id
          } else {
            depts = i.id
          }
        }
      }
      let data = {
        id:this.qywxDeptUserSelector.newsId,
        params:{toUser:userIds, toParty:depts}
      }
      releaseNews(data).then(response => {
        that.$message({
          message: '发布成功',
          type: 'success'
        });
        this.closeDeptUserSelector()
      })
    },

    openDeptUserSelector(){
      let that = this
      this.qywxDeptUserSelector.newsId = this.ids[0]
      this.qywxDeptUserSelector.selectedList = []
      this.deptUserSelectorOpen = true
      setTimeout(function(){
        that.$refs.userSelector.getDeptTree()
      },50)
    },
    closeDeptUserSelector(){
      this.deptUserSelectorOpen = false
    },
    setSelectedList(item, val){
      let findIndex = this.qywxDeptUserSelector.selectedList.findIndex(function(obj){
        return (obj.userId && obj.userId == item.userId) || (!obj.userId && obj.id == item.id && obj.label == item.label)
      })
      if(val){
        if(findIndex==-1){
          this.qywxDeptUserSelector.selectedList.push(item)
        }
      }else{
        this.qywxDeptUserSelector.selectedList.splice(findIndex,1)
      }
    },
    delSelectedList(item){
      let findIndex = this.qywxDeptUserSelector.selectedList.findIndex(function(obj){
        return (obj.userId && obj.userId == item.userId) || (!obj.userId && obj.id == item.id && obj.label == item.label)
      })
      if(findIndex>-1){
        this.qywxDeptUserSelector.selectedList.splice(findIndex,1)
      }
    }
  }
};
</script>
